package com.haoshuang.zhouzhoubang.ui.page.account.register

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.ActivityRegisterBinding
import com.haoshuang.zhouzhoubang.ui.page.help.detail.HelpDetailActivity
import com.haoshuang.zhouzhoubang.ui.page.main.MainActivity
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.CountDownWorker
import com.mufeng.libs.utils.ktx.hidePassword
import com.mufeng.libs.utils.ktx.showPassword
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger


/**
 *
 */
class RegisterActivity : BaseVMActivity<RegisterViewModel, ActivityRegisterBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<RegisterActivity>()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "注册"
        binding.titleBar.leftImageView().click { finish() }

        binding.tvGetCode.clickWithTrigger {
            val phone = binding.etPhone.text.toString()
            if (phone.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }
            if (phone.length != 11) {
                toast("请输入正确的手机号")
                return@clickWithTrigger
            }
            vm.sendSMS(phone)
        }

        binding.cbPassword.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                binding.etPassword.showPassword()
            } else {
                binding.etPassword.hidePassword()
            }
        }

        binding.cbPasswordAgain.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                binding.etPasswordAgain.showPassword()
            } else {
                binding.etPasswordAgain.hidePassword()
            }
        }

        binding.tvSpareGetCode.clickWithTrigger {
            val phone = binding.etPhone.text.toString()
            val sparePhone = binding.etSparePhone.text.toString()
            if (phone.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }
            if (phone.length != 11) {
                toast("请输入正确的手机号")
                return@clickWithTrigger
            }
            if (sparePhone.isEmpty()) {
                toast("请输入备用手机号")
                return@clickWithTrigger
            }
            if (sparePhone.length != 11) {
                toast("请输入正确的备用手机号")
                return@clickWithTrigger
            }
            if (sparePhone == phone) {
                toast("备用手机号不能和手机号一样")
                return@clickWithTrigger
            }
            vm.getSpareCode(sparePhone)
        }

        binding.tvPrivacyPolicy.clickWithTrigger {
            startPage<HelpDetailActivity>(
                "title" to "隐私政策",
                "url" to URLConstants.privacyPolicy
            )
        }

        binding.tvRegister.clickWithTrigger {
            val phone = binding.etPhone.text.toString()
            if (phone.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }
            if (phone.length != 11) {
                toast("请输入正确的手机号")
                return@clickWithTrigger
            }
            val code = binding.etCode.text.toString()
            if (code.isEmpty()) {
                toast("请输入验证码")
                return@clickWithTrigger
            }
            val password = binding.etPassword.text.toString()
            if (password.isEmpty()) {
                toast("请输入密码")
                return@clickWithTrigger
            }
            val passwordAgain = binding.etPasswordAgain.text.toString()
            if (passwordAgain.isEmpty()) {
                toast("请输入确认密码")
                return@clickWithTrigger
            }
            if (password != passwordAgain) {
                toast("两次密码不一致")
                return@clickWithTrigger
            }
            val sparePhone = binding.etSparePhone.text.toString()
            val spareCode = binding.etSpareCode.text.toString()
            if (sparePhone.isEmpty()) {
                toast("请输入备用手机号")
                return@clickWithTrigger
            }
            if (sparePhone.length != 11) {
                toast("请输入正确的备用手机号")
                return@clickWithTrigger
            }
            if (sparePhone == phone) {
                toast("备用手机号不能和手机号一样")
                return@clickWithTrigger
            }
            if (spareCode.isEmpty()) {
                toast("请输入备用验证码")
                return@clickWithTrigger
            }
            if (!binding.cbAgreement.isChecked) {
                toast("请同意隐私政策")
                return@clickWithTrigger
            }
            vm.register(phone, code, password, sparePhone, spareCode)
        }
    }

    override fun initData() {

    }

    override fun startObserve() {
        vm.sendSMSData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("验证码发送成功")
                binding.tvGetCode.isEnabled = false
                CountDownWorker(this@RegisterActivity, total = 60, onChange = {
                    binding.tvGetCode.text = "${it}S"
                }, onFinish = {
                    binding.tvGetCode.isEnabled = true
                    binding.tvGetCode.text = "重新获取"
                })
                    .start()
            }
        }

        vm.getSpareCodeData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                binding.tvSpareGetCode.isEnabled = false
                CountDownWorker(this@RegisterActivity, total = 60, onChange = {
                    binding.tvSpareGetCode.text = "${it}S"
                }, onFinish = {
                    binding.tvSpareGetCode.isEnabled = true
                    binding.tvSpareGetCode.text = "重新获取"
                }).start()
            }
        }

        vm.registerData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("注册成功")
                startPage<MainActivity>()
                AccountUtils.user = it?.data
                AccountUtils.token = it?.data?.token ?: ""
                finish()
            }
        }
    }

}